//
//  FirstScroll.m
//  轮胎商城
//
//  Created by 张晋磊 on 2017/3/23.
//  Copyright © 2017年 张晋磊. All rights reserved.
//

#import "FirstScroll.h"

@interface FirstScroll ()<UIScrollViewDelegate>
{
    int ii;
}
@end

@implementation FirstScroll

-(instancetype)initWithFrame:(CGRect)frame{

    if(self = [super initWithFrame:frame]){
        
        self.pagingEnabled = YES;
        self.delegate = self;
        ii=0;
//        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(repeatsImage) userInfo:nil repeats:YES];


    }
    return self;
}


-(void)setArrImg:(NSArray *)arrImg{

    _arrImg = arrImg;
    
    if(_arrImg.count > 0){
    
        self.contentSize = CGSizeMake((_arrImg.count + 2) * self.frame.size.width, 200);
        
        for(int i=0;i<_arrImg.count;i++){
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * (i + 1), 0, self.frame.size.width, self.frame.size.width)];
            img.tag = 1000+i;
            img.contentMode = UIViewContentModeScaleAspectFill;
            
            img.image = [UIImage imageNamed:_arrImg[i]];
            
            [self addSubview:img];
        }
        

    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * (1 + _arrImg.count), 0, self.frame.size.width, self.frame.size.height)];
    
    img.image = [UIImage imageNamed:_arrImg[0]];
    
    [self addSubview:img];
    
    
    if(self.contentOffset.x == img.frame.origin.x){
        
        self.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
    NSLog(@"%f %f",self.contentOffset.x,img.frame.origin.x);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    
    
}


-(void)repeatsImage{

    ii++;
    
    if(ii < _arrImg.count){
    
        [self setContentOffset:CGPointMake(self.frame.size.width * ii, 0) animated:YES];
    }
    
    if(ii == _arrImg.count){
    
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * ii, 0, self.frame.size.width, self.frame.size.height)];
        
        img.image = [UIImage imageNamed:_arrImg[0]];
        [self addSubview:img];
        
//        self.contentSize = CGSizeMake(self.frame.size.width * (ii+1), self.frame.size.height);
        
        [self setContentOffset:CGPointMake(self.frame.size.width * ii, 0) animated:YES];
        
        ii=0;
    }
    
    
    
}




















@end
